package com.quwan.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.*;

/**
 * @author quan
 * @date 2021-10-22 17:34
 */
@Slf4j
@Configuration
public class ThreadPoolConfig {
    private final static Integer ASYNC_CORE = 4;
    private final static Integer ASYNC_MAX = 32;
    private final static Integer ASYNC_KEEP_ALIVE_TIME = 120;
    private final static Integer ASYNC_QUEUE_MAX = ASYNC_MAX * 1024;

    @Bean("asyncThread")
    public ThreadPoolTaskExecutor asyncThread() {
        //  ThreadFactory threadFactory = Executors.defaultThreadFactory();
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(ASYNC_CORE);
        executor.setMaxPoolSize(ASYNC_MAX);
        executor.setKeepAliveSeconds(ASYNC_KEEP_ALIVE_TIME);
        executor.setQueueCapacity(ASYNC_QUEUE_MAX);
        executor.setThreadNamePrefix("async-pool-");
        return executor;
    }




}


